// pages/cupOrder/cupOrder.js
const app = getApp();
import { u_formatTimestamp } from '../../config/tool.js'
import { vipReceived } from '../../config/https.js';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isNone: false, // 是否为空
    type: 1, // 1商家配送 2自提
    list: [], // 列表
    page: 0,
    pagesize: 10,
    noMore: false, // 没有更多了
    getList: [], // 发货、提货记录列表
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.initData();
    this.getData();
  },

  // 获取数据
  getData() {
    app.loading();
    let that = this;
    wx.request({
      url: app.baseURL + 'order/getPackageList',
      data: {
        type: that.data.type,
        token: app.globalData.userId,
        page: ++that.data.page,
        page_size: that.data.pagesize
      },
      success(data) {
        let res = data.data;
        wx.hideLoading();
        if (res.code == 1) {
          if (res.data.list.length > 0) {
            res.data.list.forEach((item) =>{
              item.showed = false
            })
            that.setData({
              list: [...that.data.list, ...res.data.list],
              noMore: Number(res.data.list.length) >= Number(that.data.pagesize) ? false : true
            })
          }

          if (that.data.list.length > 0) {
            that.setData({
              isNone: false,
            })
          } else {
            that.setData({
              isNone: true,
              list: [],
            })
          }
        } else {
          app.$tip(res.msg);
          that.setData({
            isNone: true
          })
        }
      }
    })
  },

  // 格式化
  initData() {
    this.setData({
      list: [], // 列表
      page: 0,
    })
  },

  // 改变订单类型 1配送 2自提
  changeType(e) {
    this.setData({
      type: e.currentTarget.dataset.type,
      list: [],
      page: 0
    })
    this.getData();
  },

  // 获取发货记录
  getHis(e){
    let id = e.currentTarget.dataset.id, that = this, index = e.currentTarget.dataset.index, old = this.data.list;

    if (old[index].showed){
      old[index].showed = false,
      this.setData({
        list: old
      });
      return
    }
    app.loading();
    wx.request({
      url: app.baseURL + 'order/getDeliveryRecord',
      data: {
        token: app.globalData.userId,
        order_id: id
      },
      success(data){
        let res = data.data;
        wx.hideLoading();
        if(res.code == 1){
          if (res.data.list.length > 0) {
            res.data.list.forEach((item) => {
              item.create_time = u_formatTimestamp(item.create_time * 1000)
            })
            old[index].showed = true;
            that.setData({
              getList: res.data.list,
              list: old
            })
          } else {
            app.$tip('暂时没有发货记录哦~');
          }
        }else{
          app.$tip(res.msg);
        }
      }
    })
  },

  // 获取提货记录
  getRecive(e){
    let id = e.currentTarget.dataset.id, that = this, index = e.currentTarget.dataset.index, old = this.data.list;

    if (old[index].showed) {
      old[index].showed = false,
        this.setData({
          list: old
        });
      return
    }

    app.loading();
    wx.request({
      url: app.baseURL + 'order/getWriteOffRecord',
      data: {
        token: app.globalData.userId,
        order_id: id
      },
      success(data){
        let res = data.data;
        wx.hideLoading();
        if (res.code == 1) {
          if (res.data.list.length > 0) {
            res.data.list.forEach((item) => {
              item.create_time = u_formatTimestamp(item.create_time * 1000)
            })
            old[index].showed = true;
            that.setData({
              getList: res.data.list,
              list: old
            })
          } else {
            app.$tip('暂时没有提货记录哦~');
          }
        }else{
          app.$tip(res.msg);
        }
      }
    })
  },

  // 查看详情
  toDetail(e) {
    let orderid = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/cupdetail/cupdetail?id=' + orderid,
    })
  },

  // 确认收货
  recived(e) {
    let id = e.currentTarget.dataset.id, that = this;
    wx.showModal({
      title: '温馨提示',
      content: '您确定要确认收货吗?',
      confirmColor: '#5CA032',
      success(res) {
        if (res.confirm) {
          app.loading();
          vipReceived({
            id: id,
            token: app.globalData.userId
          }).then(ress => {
              wx.hideLoading();
              if (ress.code == 1) {
                app.$tip(ress.msg)
                setTimeout(() => {
                  that.initData();
                  that.getData();
                }, 1500)
              } else {
                app.$tip(ress.msg)
              }
          })
        }
      }
    })
  },

  // 查看物流
  toLogistics(e){
    let orderid = e.currentTarget.dataset.order_id,id= e.currentTarget.dataset.id;
    if(orderid){
      wx.navigateTo({
        url: '/pages/logistics/logistics?id=' + id + '&order_id=' + orderid,
      })
    }else{
      wx.navigateTo({
        url: '/pages/logistics/logistics?order_id=' + id,
      })
    }
  },

  // 去提货
  toRecive(e){
    wx.navigateTo({
      url: '/pages/receiveCode/receiveCode?id='+e.currentTarget.dataset.id,
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (!this.data.noMore) {
      // this.setData({
      //   page: this.data.page + 1
      // })
      this.getData();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})